import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '../store/page';
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/login'),
    meta: {
      title: '登录'
    }
  },
  {
    name: 'home',
    path: '/home',
    component: () => import('@/views/home'),
  },
  {
    name: 'applyAddTesting',
    path: '/check/applyAddTesting',
    component: () => import('@/views/check/applyAddTesting'),
    meta: {
      title: '加测申请信息'
    },
    pcurl: 'Lims-SampleCheck-ApplynAddTesting'
  },
  {
    name: 'addTestApplyInfo',
    path: '/addTestApplyInfo',
    component: () => import('@/views/check/applyAddTesting/components/addTestApplyInfo.vue'),
    meta: {
      title: '加测申请'
    }
  },
  {
    name: 'addTestAllocation',
    path: '/addTestAllocation',
    component: () => import('@/views/check/applyAddTesting/components/addTestAllocation.vue'),
    meta: {
      title: '加测分配管理'
    }
  },
  {
    name: 'reportUpdate',
    path: '/check/reportUpdate',
    component: () => import('@/views/check/reportUpdate'),
    meta: {
      title: '报告更新管理'
    },
    pcurl: 'Lims-SampleCheck-ReportUpdate'
  },
  {
    name: 'addReportUpdateInfo',
    path: '/addReportUpdateInfo',
    component: () => import('@/views/check/reportUpdate/components/addReportUpdateInfo.vue'),
    meta: {
      title: '新增报告更新信息'
    }
  },
  {
    name: 'addUpdateContentInfo',
    path: '/addUpdateContentInfo',
    component: () => import('@/views/check/reportUpdate/components/addUpdateContentInfo.vue'),
    meta: {
      title: '报告更新内容填写'
    }
  },
  // {
  //   name: 'checkApply',
  //   path: '/check/checkApply',
  //   component: () => import('@/views/check/checkApply'),
  //   meta: {
  //     title: '产品请验'
  //   },
  //   pcurl: 'Lims-SampleCheck-checkProcessMaterial'
  // },
  {
    name: 'ApplyList',
    path: '/ApplyList',
    component: () => import('@/views/check/checkApply/components/ApplyList.vue'),
    meta: {
      title: '产品请验列表'
    },
  },
  {
    name: 'ApplyForm',
    path: '/ApplyForm',
    component: () => import('@/views/check/checkApply/components/ApplyForm.vue'),
    meta: {
      title: '产品请验'
    },
  },
  {
    name: 'mySampling',
    path: '/check/mySampling',
    component: () => import('@/views/check/mySampling'),
    meta: {
      title: '待我采样'
    },
    pcurl: 'Lims-SampleCheck-MySampling'
  },
  {
    name: 'addSampleItem',
    path: '/addSampleItem',
    component: () => import('@/views/check/mySampling/addSampleItem.vue'),
    meta: {
      title: '取样记录(自制)'
    }
  },
  {
    name: 'addStabilityItem',
    path: '/addStabilityItem',
    component: () => import('@/views/check/mySampling/addStabilityItem.vue'),
    meta: {
      title: '稳定性取样记录'
    }
  },
  {
    name: 'addSampleCompany',
    path: '/addSampleCompany',
    component: () => import('@/views/check/mySampling/addSampleCompany.vue'),
    meta: {
      title: '取样记录(外购)'
    }
  },
  {
    name: 'addSampleCompany',
    path: '/addSampleCompany',
    component: () => import('@/views/check/mySampling/addSampleCompany.vue'),
    meta: {
      title: '取样记录(外购)'
    }
  },
  {
    name: 'checkAssign',
    path: '/check/checkAssign',
    component: () => import('@/views/check/checkAssign'),
    meta: {
      title: '检验分配'
    },
    pcurl: 'Lims-SampleCheck-checkAssign'
  },
  {
    name: 'sampleSeparation',
    path: '/check/sampleSeparation',
    component: () => import('@/views/check/sampleSeparation'),
    meta: {
      title: '样品分样'
    },
    pcurl: 'Lims-SampleCheck-SampleSeparation'
  },
  {
    name: 'myCheckObject',
    path: '/check/myCheckObject',
    component: () => import('@/views/check/myCheckObject'),
    meta: {
      title: '待我检验',
      keepAlive: true
    },
    pcurl: 'Lims-SampleCheck-myCheckObject'
  },
	  {
    name: 'microbialRegistration',
    path: '/check/microbialRegistration',
    component: () => import('@/views/check/microbialRegistration'),
    meta: {
      title: '微生物登记',
      keepAlive: true
    },
    pcurl: 'Lims-SampleCheck-microbialRegistration'
  },
  {
    name: 'checkResultProof',
    path: '/check/checkResultProof',
    component: () => import('@/views/check/checkResultProof'),
    meta: {
      title: '检验校对'
    },
    pcurl: 'Lims-SampleCheck-checkResultProof'
  },
  {
    name: 'inspectionReview',
    path: '/inspectionReview',
    component: () => import('@/views/check/InspectionReview'),
    meta: {
      title: '记录复核'
    },
    pcurl: 'Lims-SampleCheck-InspectionReview'
  },
  {
    name: 'checkModify',
    path: '/check/checkModify',
    component: () => import('@/views/check/checkModify'),
    meta: {
      title: '检验修改'
    },
    pcurl: 'Lims-SampleCheck-checkModify'
  },
  {
    name: 'checkApprove',
    path: '/check/checkApprove',
    component: () => import('@/views/check/checkApprove'),
    meta: {
      title: '检验审批'
    },
    pcurl: 'Lims-SampleCheck-CheckApprove'
  },
  {
    name: 'configDetail',
    path: '/configDetail',
    component: () => import('@/views/record/configDetail.vue'),
    meta: {
      title: '检验详情',
			keepAlive: true
    }
  },
  {
    name: 'recordQuery',
    path: '/recordQuery',
    component: () => import('@/views/record/recordQuery.vue'),
    meta: {
      title: '电子记录查询'
    }
  },
  {
    name: 'recordDetail',
    path: '/recordDetail',
    component: () => import('@/views/record/recordDetail.vue'),
    meta: {
      title: '电子记录查详情'
    }
  },
  {
    name: 'waterWaitForInspection',
    path: '/microBial/waterWaitForInspection',
    component: () => import('@/views/microBial/waterWaitForInspection'),
    meta: {
      title: '公用系统待我检验',
      keepAlive: true
    },
    pcurl: 'Lims-MicroBial-waterWaitForInspection'
  },
  {
    name: 'waterWaitForInspectionDetail',
    path: '/microBial/waterWaitForInspectionDetail',
    component: () => import('@/views/microBial/waterWaitForInspectionDetail'),
    meta: {
      title: '公用系统检验详情',
      keepAlive: true
    },
    pcurl: 'Lims-MicroBial-waterWaitForInspection'
  },
  {
    name: 'weightItem',
    path: '/weightItem',
    component: () => import('@/views/weight/weightItem.vue'),
    meta: {
      title: '项目称重'
    }
  },
  {
    name: 'weightItemList',
    path: '/weightItemList',
    component: () => import('@/views/weight/weightItemList.vue'),
    meta: {
      title: '称重项目列表'
    }
  },
  {
    name: 'pdfUpload',
    path: '/pdfUpload',
    component: () => import('@/views/record/pdfUpload.vue'),
    meta: {
      title: 'PDF取值'
    }
  },
  {
    name: 'recordQuote',
    path: '/recordQuote',
    component: () => import('@/views/record/recordQuote.vue'),
    meta: {
      title: '引用'
    },
		
  },  
	{
    name: 'fileGrabOrUpload',
    path: '/fileGrabOrUpload',
    component: () => import('@/views/fileGrabOrUpload/index.vue'),
    meta: {
      title: '附件上传'
    }
	},  
	{
    name: 'weight',
    path: '/weight',
    component: () => import('@/views/record/weight.vue'),
    meta: {
      title: '称重'
    }
	}
]

const router = new VueRouter({
  routes
})

/**
 * 设置页面缓存：
 * 1.路由中的meta.keepAlive设置为true，
 * 2.路由中的name需和对应组件的name保持一致
 */
router.beforeEach((to, from, next) => {
  // 如果是登录页，重置访问记录和缓存组件数组
  if (to.path === '/home') {
    store.commit('SET_VISITED_ROUTES', [])
    store.commit('SET_CACHES', [])
  }
  // 判断当前路由是否需要缓存
  console.log(to.meta);
  if (!store.state.caches.includes(to.name) && to.meta.keepAlive && to.name) {
    store.commit('ADD_CACHES', to.name);
  }
  next()
});
router.afterEach((to, from,) => {
  // 判断要去的页面路由是否已经在数组中存在，存在将当前页面的浏览记录删除，否则就是浏览新页面
  if (store.state.visitedRoutes.includes(to.path)) {
    // 返回之前的页面将当前页面的缓存清除
    if (from.name && store.state.caches.includes(from.name)) store.commit('REMOVE_CACHES', from.name);
    store.commit('REMOVE_VISITED_ROUTES', from.path);
  } else {
    store.commit('ADD_VISITED_ROUTES', to.path);
  }
});
export default router
export { routes };
